from chiori import Config
from .utils import check_params

path = 'data/minigame/materials.toml'
item_model = {'name':'< ??? >', 'price':100, 'sell_price':50, 'buy_price':130,
	'tier':1, 'craft':{}}

class Material(object):
	"""docstring for Material"""
	def __init__(self, item_id):
		super(Material, self).__init__()
		self.item_id = item_id
		self.index = Config(None, {'stone':{'name':'булыжник'}}, path).file_data

		self.data = self.get_item_data()
		self.__dict__.update(self.data)


	def status(self):
		return f'[{self.tier}:{self.item_id}] {self.name}'


	def get_item_data(self, item_id=None):
		if not item_id:
			item_id = self.item_id

		if item_id in self.index:
			data = self.index[item_id]
		else:
			data = {}

		if not 'price' in data and 'craft' in data:
			price = 0
			for k, v in data["craft"]["items"].items():
				price += Material(k).price * v

			if not 'count' in data["craft"]:
				data["craft"]["count"] = 1
			data["price"] = round(price / data["craft"]["count"])

		if 'price' in data:
			data["sell_price"] = round(data["price"] * 0.5)
			data["buy_price"] = round(data["price"] * 1.15)

		return check_params(data, item_model)
